<?php
/**
 * Lib\Pidguin\Autoloader - class loader file
 *
 * @author Eni
 * @version 0.1.1
 */
class Pidguin_Autoloader
{
	/**
	 * Register autoload
	 * 
	 * @return void
	 */
	public static function registerAutoload()
	{
		spl_autoload_register(array(new self(), 'autoload'));
		set_include_path(LIB_DIR);
	}
	
	/**
	 * Autoloader class
	 *
	 * @return void
	 */
	public static function autoload($className)
	{
		$getPath = function() use(&$className) {
			$classSeparator = strpos($className, '_');
			$class = substr($className, 0, $classSeparator);
			switch($class) {
				case 'Pidguin':
					return LIB_DIR . DS;
					break;
				case 'Model':
					return APP_DIR . DS;
					break;
				case 'Application':
					$className = substr($className, $classSeparator);
					return APP_DIR . DS;
					break;
				case 'Zend':
					return LIB_DIR . DS;
					break;
				case 'Plugin':
					return APP_DIR . DS . 'Plugins';
					break;
				default:
					return ROOT_DIR . DS;
					break;
			}
		};
		
		$pathToClass = $getPath() . trim(strtr($className, array('\\' => DS, '_' => DS)), DS) . '.php';
		if (!is_file($pathToClass)) {
			throw new Exception("File $pathToClass not found");
		}
		
		require $pathToClass;
	}
}
